import commands
from pyspades.server import hit_packet
from commands import get_player
from twisted.internet import reactor

@commands.name('stats')
def stats(connection, value = None):
    if value == None:
        reactor.callLater(0.0,connection.send_chat,'Your Stats:')
        reactor.callLater(1.0,connection.send_chat,'%s kills, %s deaths, %s caps' % (connection.kill_count,connection.death_count,connection.cap_count))
        reactor.callLater(2.0,connection.send_chat,'%s suicides, %s teamkills, teamkilled %s times' % (connection.suicide_count,connection.tk_count,connection.victim_count))
        reactor.callLater(3.0,connection.send_chat,'%s blocks placed, %s blocks destroyed' % (connection.build_count,connection.destroy_count))
    else:
        player = get_player(connection.protocol, value)
        reactor.callLater(0.0,connection.send_chat,'Stats for %s:' % player.name)
        reactor.callLater(1.0,connection.send_chat,'%s kills, %s deaths, %s caps' % (player.kill_count,player.death_count,player.cap_count))
        reactor.callLater(2.0,connection.send_chat,'%s suicides, %s teamkills, teamkilled %s times' % (player.suicide_count,player.tk_count,player.victim_count))
        reactor.callLater(3.0,connection.send_chat,'%s blocks placed, %s blocks destroyed' % (player.build_count,player.destroy_count))
@commands.name('serverstats')
def serverstats(connection):
    reactor.callLater(0.0,connection.send_chat,'Server Stats:')
    reactor.callLater(1.0,connection.send_chat,'%s kills, %s deaths, %s caps, %s tks' % (connection.protocol.kill_count,connection.protocol.death_count,connection.protocol.cap_count,connection.protocol.tk_count))
    reactor.callLater(3.0,connection.send_chat,'%s blocks placed, %s blocks destroyed' % (connection.protocol.build_count,connection.protocol.destroy_count))
commands.add(stats)
commands.add(serverstats)


def apply_script(protocol, connection, config):
    class StatConnection(connection):
        connection.build_count = 0
        connection.destroy_count = 0
        connection.cap_count = 0
        connection.kill_count = 0
        connection.death_count = 0
        connection.suicide_count = 0
        connection.tk_count = 0
        connection.victim_count = 0

        def on_block_removed(self, x, y, z):
            self.destroy_count += 1
            self.protocol.destroy_count += 1
            return connection.on_block_removed(self, x, y, z)

        def on_block_build(self, x, y, z):
            self.build_count += 1
            self.protocol.build_count += 1
            return connection.on_block_build(self, x, y, z)

        def on_kill(self, killer):
            if killer:
                self.death_count += 1
                self.protocol.death_count += 1
                if self == killer:
                    self.suicide_count += 1
                elif self.team == killer.team:
                    self.victim_count +=1
                    killer.tk_count += 1
                    self.protocol.tk_count += 1
                else:
                    killer.kill_count += 1
                    self.protocol.kill_count += 1
            return connection.on_kill(self, killer)

        def on_capture_flag(self):
            self.cap_count += 1
            self.protocol.cap_count += 1
            return connection.on_capture_flag(self)
    class StatProtocol(protocol):
        build_count = 0
        destroy_count = 0
        cap_count = 0
        kill_count = 0
        death_count = 0
        tk_count = 0
    return StatProtocol, StatConnection